import * as Blockly from 'blockly/core';

const blocks = {};

const BLOCK_TOOL_INPUT_HUE = 210;

for (let inputObj of [{
    type: 'ValueInput',
    name: 'block_tool_value_input',
    text: '值输入'
}, {
    type: 'StatementInput',
    name: 'block_tool_statement_input',
    text: '声明输入'
}, {
    type: 'DummyInput',
    name: 'block_tool_dummy_input',
    text: '伪输入'
}]) {
    blocks[inputObj.name] = {
        init: function () {
            const dummyInputObj = this.appendDummyInput();
            dummyInputObj.appendField(inputObj.text + (inputObj.type !== 'DummyInput' ? ':' : ''));
            if (inputObj.type !== 'DummyInput') {
                dummyInputObj.appendField(new Blockly.FieldTextInput("NAME"), "NAME");
            }
            this.appendStatementInput("VALUE")
                .setCheck("BLOCK_TOOL_INPUT_BLOCK")
                .appendField("添加组件")
                .appendField(new Blockly.FieldDropdown([
                    ["左对齐", "Blockly.ALIGN_LEFT"],
                    ["右对齐", "Blockly.ALIGN_RIGHT"],
                    ["居中对齐", "Blockly.ALIGN_CENTRE"]
                ]), "ALIGN_TYPE");
            this.appendValueInput("ALLOW_TYPE")
                .setCheck(null)
                .setAlign(Blockly.ALIGN_RIGHT)
                .appendField("类型:");
            this.setInputsInline(false);
            this.setPreviousStatement(true, "BLOCK_TOOL_MAIN_BLOCK");
            this.setNextStatement(true, "BLOCK_TOOL_MAIN_BLOCK");
            this.setColour(BLOCK_TOOL_INPUT_HUE);
            this.setTooltip("");
            this.setHelpUrl("");
        },
        onchange: function () {
            const parent = this.getSurroundParent();
            if (parent && parent.type === 'block_tool_main') {
                this.setWarningText(null);
            } else {
                this.setWarningText("此块需放到Block下面");
            }
        }
    };
}

export default blocks;